using UnityEngine;
using System;

public class CameraController : SingletonMonoBehaviour<CameraController>
{
    public float _minSpeed;
    /// <summary>
    /// The distance between main character and the camera
    /// </summary>
    public Vector3 _photographyDistance;
    private Rigidbody2D _RB;

    private void Awake() { INI_Singleton(); }

    void Start()
    {
        _RB = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        CameraMove(MainRole.Instance.gameObject.transform.position + _photographyDistance, _minSpeed);
    }

    /// <summary>
    /// Camera move on the XZ plat.
    /// </summary>
    /// <param name="start"></param>
    /// <param name="destination"></param>
    /// <param name="minSpeed"></param>
    private void CameraMove(Vector3 destination, float minSpeed)
    {
        Vector3 distance = destination - gameObject.transform.position;
        gameObject.transform.position = Math.Abs(distance.magnitude) < 0.01 * minSpeed ? destination : gameObject.transform.position;//anti shaking
        _RB.velocity = distance.magnitude != 0 ?
            distance.magnitude < minSpeed ? distance.normalized * minSpeed : distance
            : Vector3.zero;
    }
}
